from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report

# 加载数据
iris = load_iris()
X = iris.data           # 特征：花萼、花瓣长度宽度
y = iris.target         # 标签：0=setosa, 1=versicolor, 2=virginica

# 划分训练/测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 建立多分类逻辑回归模型
model = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=200)
# multi_class='multinomial' 表示用 softmax 策略（多类softmax）
# solver='lbfgs' 支持多类 softmax 求解

# 模型训练
model.fit(X_train, y_train)

# 预测
y_pred = model.predict(X_test)

# 评估
print("准确率：", accuracy_score(y_test, y_pred))
print("\n分类报告：\n", classification_report(y_test, y_pred, target_names=iris.target_names))
